package com.hethongviet.vending.bluetooth;

import com.hethongviet.vending.AppMain;
import com.hethongviet.vending.AppConfig;
import com.hethongviet.vending.model.MachineInfo;

public final class BluetoothUtil
{
	public static void start()
	{
		MachineInfo machine = AppMain.getInstance().getMachineInfo();
		if (AppConfig.IS_BLUETOOTH_SERIAL)
		{
			BluetoothSerialService.getInstance().prepareAndConnect(machine.getAddressPhysicalSerial());
		}
		else
		{
			BluetoothService.getInstance().prepareAndConnect(machine.getAddressPhysical());
		}
	}

	public static void stop()
	{
		if (AppConfig.IS_BLUETOOTH_SERIAL)
		{
			BluetoothSerialService.getInstance().stop();
		}
		else
		{
			BluetoothService.getInstance().stop();
		}
	}

	public static void restartConnect()
	{
		stop();
		start();
	}

	public static boolean isConnected()
	{
		if (AppConfig.IS_BLUETOOTH_SERIAL)
		{
			return BluetoothSerialService.getInstance().getState() == BluetoothConstants.STATE_CONNECTED;
		}
		else
		{
			return BluetoothService.getInstance().getState() == BluetoothConstants.STATE_CONNECTED;
		}
	}

	public static void setListener(OnBluetoothListener bluetoothListener)
	{
		if (AppConfig.IS_BLUETOOTH_SERIAL)
		{
			BluetoothSerialService.getInstance().setOnBluetoothListener(bluetoothListener);
		}
		else
		{
			BluetoothService.getInstance().setOnBluetoothListener(bluetoothListener);
		}
	}

	public static boolean writeMessage(byte[] byteMessage)
	{
		if (!isConnected())
		{
			return false;
		}

		if (AppConfig.IS_BLUETOOTH_SERIAL)
		{
			BluetoothSerialService.getInstance().write(byteMessage);
		}
		else
		{
			BluetoothService.getInstance().write(byteMessage);
		}
		return true;
	}

}
